/**
 * elancom Corporation copy right 2012 - 2015  
 *   
 * $Header: https://192.168.1.224/svn/elancom/product/elancom-platform/trunk/src/main/java/com/elancom/framework/web/PageState.java 1034 2012-07-02 03:09:12Z lanzhouxue $
 * $Id: PageState.java 1034 2012-07-02 03:09:12Z lanzhouxue $
 * $Author: lanzhouxue $
 * $Date: 2012-07-02 11:09:12 +0800 (周一, 02 七月 2012) $
 * $Revision: 1034 $
 */
package com.elancom.framework.web;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.PageContext;

/**
 * @author elancom
 * @date 2012-5-13
 */
public class PageState {
	private static final String PAGE_STATE_KEY = "$pageState";

	public static final String SELECT_TEXT = "查询";

	public static final String DELETE_TEXT = "删除";

	public static final String ADD_TEXT = "新增";

	public static final String UPDATE_TEXT = "修改";

	public static final String VIEW_TEXT = "查看";

	public static final String SELECT = "select";

	public static final String DELETE = "delete";

	public static final String ADD = "add";

	public static final String UPDATE = "update";

	public static final String VIEW = "view";

	private String stateName;

	private String stateCnName;

	private String requestPath;

	private RequestDelegate requestDelegate;

	public static PageState get(RequestDelegate requestDelegate) {
		PageState state = null;
		if ((state = (PageState) requestDelegate.getAttribute(PAGE_STATE_KEY)) == null) {
			state = new PageState();
			requestDelegate.setAttribute(PAGE_STATE_KEY, state);
			state.requestDelegate = requestDelegate;
		}
		return state;
	}

	public static PageState get(ServletRequest request) {
		return (PageState) request.getAttribute(PAGE_STATE_KEY);
	}

	public static PageState get(PageContext pageContext) {
		return (PageState) pageContext.getAttribute(PAGE_STATE_KEY);
	}

	public static PageState get(HttpSession session) {
		return (PageState) session.getAttribute(PAGE_STATE_KEY);
	}

	public static PageState get(ServletAttribute attribute) {
		return (PageState) attribute.getAttribute(PAGE_STATE_KEY);
	}

	private PageState() {
	}

	public String getRequestPath() {
		return requestPath;
	}

	public String getState() {
		return stateName;
	}

	public PageState setAddState() {
		stateName = ADD;
		return this;
	}

	public PageState setDeleteState() {
		stateName = DELETE;
		return this;
	}

	public PageState setRequestPath(String requestPath) {
		this.requestPath = requestPath;
		return this;
	}

	public PageState setSelectState() {
		stateName = SELECT;
		return this;
	}

	public PageState setViewState() {
		stateName = VIEW;
		return this;
	}

	public PageState setState(String stateName) {
		this.stateName = stateName;
		return this;
	}

	public PageState setUpdateState() {
		stateName = UPDATE;
		return this;
	}

	public String getStateCnName() {
		if (stateCnName == null || stateCnName.trim().length() == 0) {
			if (UPDATE.equals(stateName)) {
				return UPDATE_TEXT;
			}

			if (ADD.equals(stateName)) {
				return ADD_TEXT;
			}

			if (DELETE.equals(stateName)) {
				return DELETE_TEXT;
			}

			if (SELECT.equals(stateName)) {
				return SELECT_TEXT;
			}

			if (VIEW.equals(stateName)) {
				return VIEW_TEXT;
			}
		}

		return stateCnName;
	}

	public PageState setStateCnName(String stateCnName) {
		this.stateCnName = stateCnName;
		return this;
	}

	public PageState setAttribute(String name, Object value) {
		requestDelegate.setAttribute(name, value);
		return this;
	}

	@SuppressWarnings("unchecked")
	public <T> T getAttribute(String name) {
		return (T) requestDelegate.getAttribute(name);
	}
}
